//
//  WKGuideViewController.m
//  WeKeTang
//
//  Created by ichina on 2017/8/24.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//
static NSString *const cellID = @"guide";

#import "WKGuideViewController.h"
#import "WKNetworkTools.h"
#import <UIImageView+YYWebImage.h>
#import "UIView+Extension.h"
#import "AppDelegate.h"
#import "WKLoginViewController.h"

@interface WKGuideViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) NSArray *imageList;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) UIButton *starBtn;

@end

@implementation WKGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.imageList = @[@"tip1.png",@"tip2.png",@"tip3.png"];
    [self setupUI];
    
}
- (void)setupUI{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    self.collectionView.pagingEnabled = YES;
    flowLayout.itemSize = self.collectionView.bounds.size;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
    self.pageControl = [[UIPageControl alloc] init];
    self.pageControl.frame = CGRectMake(kDeviceScreenWidth*0.5, kDeviceScreenHeight-50, 80, 20);//指定位置大小
    self.pageControl.backgroundColor = [UIColor redColor];
    self.pageControl.numberOfPages = 3;//指定页面个数
    self.pageControl.currentPage = 0;//指
    self.pageControl.pageIndicatorTintColor = RGBColor(169, 217, 223);// 设置非选中页的圆点颜色
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor]; // 设置选中页的圆点颜色
    self.pageControl.transform=CGAffineTransformScale(CGAffineTransformIdentity, 1.3, 1.3);
    [self.view addSubview:self.pageControl];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:cell.contentView.bounds];
//    imageView.backgroundColor  = [UIColor colorWithRed:arc4random_uniform(256)/255.f green:arc4random_uniform(256)/255.f blue:arc4random_uniform(256)/255.f alpha:1];
    [cell.contentView addSubview:imageView];
    if (indexPath.row == 0) {
//        NSURL *url = [NSURL URLWithString:@"http://dl.dezhi.com/api/apk/guide_one.jpg"];
//        [imageView setImageWithURL:url options:YYWebImageOptionShowNetworkActivity];
        imageView.image = [UIImage imageNamed:@"guide_1"];
    }else if(indexPath.row == 1){
        imageView.image = [UIImage imageNamed:@"guide_2"];
    }else{
        imageView.image = [UIImage imageNamed:@"guide_3"];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    int page = scrollView.contentOffset.x / scrollView.frame.size.width;
    // 设置页码
    _pageControl.currentPage = page;
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    int page = scrollView.contentOffset.x / scrollView.frame.size.width;
//    // 设置页码
//    _pageControl.currentPage = page;
    
    if (page == 2) {
        self.starBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.starBtn.frame = CGRectMake(kDeviceScreenWidth *0.5-100, kDeviceScreenHeight-self.pageControl.height-100, 200, 50);
        self.starBtn.backgroundColor = [UIColor clearColor];
        [self.starBtn setBackgroundImage:IMG(@"icon_start") forState:UIControlStateNormal];
        [self.starBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.starBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.starBtn addTarget:self action:@selector(startAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.starBtn];
    }else{
        [self.starBtn removeFromSuperview];
    }
}
#pragma mark - 启动
- (void)startAction:(UIButton*)sender{
    [MobClick profileSignInWithPUID:@"experience_onclick"];
    [AppDelegate shareDelegate].window.rootViewController = [[WKLoginViewController alloc]init];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
